var pomelo = require('pomelo');
var routeUtil = require('./app/util/routeUtil');

var co = require('co');
/**
 * Init app for client.
 */
var app = pomelo.createApp();
app.set('name', 'chatofpomelo');

var config = require('./config/config');

co(function*() {
  require('../shared/Expand')();
  global.db = yield require('../shared/db/mongo')(config.mongodb);
  let rsmq = global.rsmq = require('../shared/MessageQueue')(config.MessageQueue);

  yield Array.prototype.map.call(config.MessageQueue.queue, function(cfg) {
    return new Promise((resolve, reject) => {
      rsmq.createQueue(cfg).then(queue => {
        resolve(queue);
      }).catch(reject);
    });
  });
  // app configure
  app.configure('production|development', function() {
    // route configures

    app.route('chat', routeUtil.chat);
    app.set('connectorConfig', {
      connector: pomelo.connectors.sioconnector,
      // 'websocket', 'polling-xhr', 'polling-jsonp', 'polling'
      transports: ['websocket', 'polling'],
      heartbeats: true,
      closeTimeout: 60 * 1000,
      heartbeatTimeout: 60 * 1000,
      heartbeatInterval: 25 * 1000
    });
    // filter configures
    app.filter(pomelo.timeout());
  });

  // start app
  app.start();

  process.on('uncaughtException', function(err) {
    console.error(' Caught exception: ' + err.stack);
  });
}).catch(e => {
  console.error(e);
});
